package banco.informativo_fiscal;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import dominio.*;
import dominio.cmv.Informativo_Fiscal;
import dominio.informativo_fiscal.Agua_Luz_Telefone;
import dominio.informativo_fiscal.Aluguel;
import dominio.informativo_fiscal.Encargos_Contribuicoes_Desp_Trib;
import dominio.informativo_fiscal.Folha_Pagamento;

import banco.Banco;
import fachada.informativo_fiscal.Fachada_Informativo_Fiscal;

public class Informativo_FiscalDAO implements Fachada_Informativo_Fiscal {

	/* (non-Javadoc)
	 * @see banco.informativo_fiscal.Fachada_Informativo_Fiscal#insertInformativo_Fiscal(dominio.Cliente, dominio.cmv.Informativo_Fiscal, dominio.informativo_fiscal.Informativo_Fiscal)
	 */
	@SuppressWarnings("finally")
	public void insertInformativo_Fiscal(Cliente c, Informativo_Fiscal info, dominio.informativo_fiscal.Informativo_Fiscal info2) throws ClassNotFoundException, IOException {
	        
	        Connection conn = null;
	        PreparedStatement stmt = null;
	        ResultSet rs = null;
	        try {
	            conn = Banco.getConexaoEXTRATOPAES();
	            stmt = conn.prepareStatement("insert into informativo_fiscal values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " +
	            		"?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
	            stmt.setFloat(1, info2.getAgua_luz_telefone().getEnergia());
	            stmt.setFloat(2, info2.getAgua_luz_telefone().getAgua());
	            stmt.setFloat(3, info2.getAgua_luz_telefone().getTelecomunicacoes());
	            stmt.setFloat(4, info2.getAgua_luz_telefone().getTotal_agua_luz_tel());
	            stmt.setFloat(5, info2.getAluguel().getAluguel());
	            stmt.setFloat(6, info2.getEcdt().getInss());
	            stmt.setFloat(7, info2.getEcdt().getFgts());
	            stmt.setFloat(8, info2.getEcdt().getContribuicao_sindical());
	            stmt.setFloat(9, info2.getEcdt().getMensalidade_sindical());
	            stmt.setFloat(10, info2.getEcdt().getTaxa_assistencial());
	            stmt.setFloat(11, info2.getEcdt().getIrpj());
	            stmt.setFloat(12, info2.getEcdt().getCsll());
	            stmt.setFloat(13, info2.getEcdt().getPis());
	            stmt.setFloat(14, info2.getEcdt().getCofins());
	            stmt.setFloat(15, info2.getEcdt().getSimples());
	            stmt.setFloat(16, info2.getEcdt().getIrrf());
	            stmt.setFloat(17, info2.getEcdt().getIcms());
	            stmt.setFloat(18, info2.getEcdt().getCpmf_iof_ioc());
	            stmt.setFloat(19, info2.getEcdt().getIss());
	            stmt.setFloat(20, info2.getEcdt().getIptu_ipva());
	            stmt.setFloat(21, info2.getEcdt().getAlvara_localizacao());
	            stmt.setFloat(22, info2.getEcdt().getOutros_valores());
	            stmt.setFloat(23, info2.getEcdt().getTotal_ECDP());
	            stmt.setFloat(24, info2.getFolha_pagamento().getSalarios());
	            stmt.setFloat(25, info2.getFolha_pagamento().getDecimo_terceiro_salario());
	            stmt.setFloat(26, info2.getFolha_pagamento().getFerias());
	            stmt.setFloat(27, info2.getFolha_pagamento().getRescisao_contrato());
	            stmt.setFloat(28, info2.getFolha_pagamento().getPro_labore());
	            stmt.setFloat(29, info2.getFolha_pagamento().getPrestacoes_servico());
	            stmt.setFloat(30, info2.getFolha_pagamento().getOutros_valores());
	            stmt.setFloat(31, info2.getFolha_pagamento().getTotal_folha_pagamento());
	            stmt.setString(32, c.getCnpj().trim());
	            stmt.setInt(33, info.getId());
	            stmt.executeUpdate();
	        }
	        catch (SQLException ex) {
	            ex.printStackTrace();
	        }
	        
	        catch (Exception e) {
	        	e.printStackTrace();
	        }
	        finally {
		        Banco.closeConexao(conn, rs, stmt);
	        }
	    }
	
	@SuppressWarnings("finally")
	public dominio.informativo_fiscal.Informativo_Fiscal selectInformativo_Fiscal(Informativo_Fiscal info) throws ClassNotFoundException, IOException {
	    
	    Connection conn = null;
	    PreparedStatement stmt = null;
	    ResultSet rs = null;
	    dominio.informativo_fiscal.Informativo_Fiscal informativo = new dominio.informativo_fiscal.Informativo_Fiscal();
	    
	    try {
	        conn = Banco.getConexaoEXTRATOPAES();
	        stmt = conn.prepareStatement("select * from informativo_fiscal where id_info_fiscal = ?");
	        stmt.setInt(1, info.getId());
	        rs = stmt.executeQuery();
	        while (rs.next()){	            
	            Agua_Luz_Telefone a = new Agua_Luz_Telefone();
	            a.setAgua(rs.getFloat("agua"));
	            a.setEnergia(rs.getFloat("energia"));
	            a.setTelecomunicacoes(rs.getFloat("telecomunicacoes"));
	            a.setTotal_agua_luz_tel(rs.getFloat("total_agua_luz_tel"));
	            Aluguel al = new Aluguel();
	            al.setAluguel(rs.getFloat("aluguel"));
	            Encargos_Contribuicoes_Desp_Trib ecdt = new Encargos_Contribuicoes_Desp_Trib();
	            ecdt.setAlvara_localizacao(rs.getFloat("alvara_localizacao"));
	            ecdt.setCofins(rs.getFloat("cofins"));
	            ecdt.setContribuicao_sindical(rs.getFloat("contribuicao_sindical"));
	            ecdt.setCpmf_iof_ioc(rs.getFloat("cpmf_iof_ioc"));
	            ecdt.setCsll(rs.getFloat("csll"));
	            ecdt.setFgts(rs.getFloat("fgts"));
	            ecdt.setIcms(rs.getFloat("icms"));
	            ecdt.setInss(rs.getFloat("inss"));
	            ecdt.setIptu_ipva(rs.getFloat("iptu_ipva"));
	            ecdt.setIrpj(rs.getFloat("irpj"));
	            ecdt.setIrrf(rs.getFloat("irrf"));
	            ecdt.setIss(rs.getFloat("iss"));
	            ecdt.setMensalidade_sindical(rs.getFloat("mensalidade_sindical"));
	            ecdt.setOutros_valores(rs.getFloat("outros_valores"));
	            ecdt.setPis(rs.getFloat("pis"));
	            ecdt.setSimples(rs.getFloat("simples"));
	            ecdt.setTaxa_assistencial(rs.getFloat("taxa_assistencial"));
	            ecdt.setTotal_ECDP(rs.getFloat("total_ECDP"));
	            Folha_Pagamento fp = new Folha_Pagamento();
	            fp.setDecimo_terceiro_salario(rs.getFloat("decimo_terceiro_salario"));
	            fp.setFerias(rs.getFloat("ferias"));
	            fp.setOutros_valores(rs.getFloat("outros_valores_FP"));
	            fp.setPrestacoes_servico(rs.getFloat("prestacoes_servico"));
	            fp.setPro_labore(rs.getFloat("pro_labore"));
	            fp.setRescisao_contrato(rs.getFloat("rescisao_contrato"));
	            fp.setSalarios(rs.getFloat("salarios"));
	            fp.setTotal_folha_pagamento(rs.getFloat("total_FP"));
	            informativo.setAgua_luz_telefone(a);
	            informativo.setAluguel(al);
	            informativo.setEcdt(ecdt);
	            informativo.setFolha_pagamento(fp);	            
	        }
	    }
	    catch (SQLException ex) {
	        ex.printStackTrace();
	    }
	    finally {
	        Banco.closeConexao(conn, rs, stmt);
	        return informativo;
	    }
	}
	
};
